﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;

using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace WitCustomControls
{
    public partial class WitButton : UserControl, IWorkItemControl
    {
        private bool _readonly;
        private string _fieldName;
        private System.Collections.Specialized.StringDictionary _properties;
        private Button _button;

        public WitButton()
        {
            InitializeComponent();
            InitControl();
        }

        #region IWorkItemControl implementation
        protected IServiceProvider _serviceProvider;
        void IWorkItemControl.SetSite(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        protected WorkItem _workItem;
        object IWorkItemControl.WorkItemDatasource
        {
            get { return _workItem; }
            set { _workItem = (WorkItem)value; }
        }

        public event EventHandler AfterUpdateDatasource;

        public event EventHandler BeforeUpdateDatasource;

        public void Clear()
        {
        }

        public void FlushToDatasource()
        {
        }

        public void InvalidateDatasource()
        {
        }

        public System.Collections.Specialized.StringDictionary Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }

        public bool ReadOnly
        {
            get { return _readonly; }
            set { _readonly = value; }
        }

        public string WorkItemFieldName
        {
            get { return _fieldName; }
            set { _fieldName = value; }
        }
        #endregion

        public string ButtonText
        {
            get { return _button.Text; }
            set { _button.Text = value; }
        }

        public int ButtonWidth
        {
            get { return _button.Width; }
            set { _button.Width = value; }
        }

        public delegate void ButtonClickHandler(object sender, EventArgs args);
        public event ButtonClickHandler ButtonClick;

        private void InitControl()
        {
            if (_button != null)
                return;

            _button = new Button();
            _button.Text = "Default Text";
            _button.Width = 125;
            _button.Top = 10;
            _button.Click += new EventHandler(_button_Click);

            base.Controls.Clear();
            base.Controls.Add(_button);
        }

        void _button_Click(object sender, EventArgs e)
        {
            if (ButtonClick != null)
                ButtonClick(sender, e);
        }
    }
}
